import { Component, OnInit, ViewChild } from '@angular/core';
import { ColumnType, SPage, TablePageComponent } from '@shared/components/table-page/table-page.component';
import { Body, _HttpClient } from '@delon/theme';
import { NzMessageService, NzModalService } from 'ng-zorro-antd';
import { UpgradeEditComponent } from '../package-detail/upgrade-edit.component'
import { ApiService } from '@core/services/api';
import { STData } from '@delon/abc';
import { ACLService } from '@delon/acl';

@Component({
  selector: 'package-list',
  templateUrl: './package-list.component.html',
  styles: []
})
export class PackageListComponent implements OnInit {
  @ViewChild('st', { static: false }) st: TablePageComponent;
  delFlag:boolean = this.aclService.canAbility("upgrade-package-list/delete");//检查用户按钮权限
  uploadFlag:boolean =  this.aclService.canAbility("upgrade-package-list/upload");
  selectData: any[];
  loading = false;
  addUserInfo = {
    userAccount: '',
    userPassword: '',
    userName: '',
    roles: [],
    depts: []
  };
  config: SPage = {
    columns: [
      {
        index: 'id',
        title: '多选',
        type: ColumnType.checkbox,
      },
      {
        index: 'upgradeName',
        title: '包名',
        filter: {
          placeholder: '请输入包名',
          type: 'string'
        }
      },
      {
        index: 'upgradeSize',
        title: '大小'
      },
      {
        index: 'upgradeFeature',
        title: '包描述'
      },
      {
        index:'upgradeType',
        title:'包类型',
        type:ColumnType.tag,
        tag:{
          1:{text:'终端升级程序'},
          2:{text:'固件升级程序'},
          3:{text:'MCU固件'},
          4:{text:'读头固件'},
          5:{text:'IPC固件'},
          6:{text:'单片机升级程序'},
        },
        filter:{
          type:'select',
          placeholder:"请选择包类型",
          items:[
            {label: '终端升级程序', value: 1},
            {label: '固件升级程序', value: 2},
            {label: 'MCU固件',value:3},
            {label: '读头固件',value:4},
            {label: 'IPC固件',value:5},
            {label: '单片机升级程序', value: 6},
          ]
        }
      },
      {
        index:'upgradeWay',
        title:'设备类型',
        type:ColumnType.tag,
        tag:{
          1:{text: '门禁摄像机'},
          2:{text: '门禁刷卡'},
          3:{text: 'IPC'},
          4:{text: 'NVR'},
          5:{text: '微型门禁产品'},
          6:{text: '安卓门禁产品'},
          7:{text: '门铃'},
          8:{text: '4G开门控制器'},
          12:{text: 'linux人脸机'}
        },
        filter:{
          type:'select',
          placeholder:"请选择设备类型",
          items:[
            {label: '门禁摄像机', value: 1},
            {label: '门禁刷卡', value: 2},
            {label: 'IPC', value: 3},
            {label: 'NVR', value: 4},
            {label: '微型门禁产品', value: 5},
            {label: '安卓门禁产品', value: 6},
            {label: '门铃', value: 7},
            {label: '4G开门控制器',value: 8},
            {label: 'linux人脸机',value: 12}
          ]
        }
      },
      {
        index: 'upgradeVersion',
        title: '版本号',
        filter: {
          placeholder: '请输入版本号',
          type: 'string'
        }
      },
      {
        index: 'updateTime',
        title: "上传时间",
        type:ColumnType.date
      }
    ],
    showCheckBox: false,
    reqUrl:'/core/upgrade/page',
    editBtn: [
      // { title: '详情' },
      {title: '删除',iif:()=> this.delFlag }
    ]
  }
  constructor(private http: _HttpClient,  private api: ApiService, private modal: NzModalService, private message: NzMessageService,private aclService: ACLService) { }
  
  ngOnInit() {

  }


  // 编辑功能开始
  addUpgrade(type: 0 | 1 = 0, _param?: object): void {
    console.log(type);
    const title = '新增升级包';
    const modal = this.modal.create({
      nzTitle: title,
      nzContent: UpgradeEditComponent,
      nzWidth: 800,
      nzComponentParams: {
        type,
        _param
      },
      nzOnOk: () => {
        this.st.reload();
      },
      nzFooter: null
    })
    modal.afterClose.subscribe(res => {
      if (res === 'ok') {
        this.st.reload();
      }
    })
  }
  //删除
  delete(param:number):void{
    console.log(param);
    const body = [];
    body.push({id:param,status:false});
    this.http.put('/core/upgrade/batch',body).subscribe(res => {
      if(res.status === '200'){
        this.message.success('删除成功');
        this.st.reload();
      }
    })
  }
  operate(e: any): void {
    
    if(e.title === '删除'){
      this.delete(e.data.id);
    }
  }
  _console(e: any): void {
    this.selectData = e;
  }
  //多条删除
  deleteAll() {
    if (!!this.selectData && this.selectData.length !== 0) {
      this.modal.confirm({
        nzTitle: '您确定要删除这些数据吗',
        nzOnOk: () => {
          const body = [];
          this.selectData.forEach(item => {
            body.push({id:item.id,status:false});
          })
          this.http.put('/core/upgrade/batch',body).subscribe(res => {
            if(res.status === '200'){
              this.message.success('删除成功');
              this.st.reload();
            }
          })
        }
      })
    } else {
      this.message.warning('请选择数据');
    }
  }
  

}
